﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace Liang.UI
{

        public class QueryHelper
        {
            /// <summary>
            /// 清空查询条件
            /// </summary>
            /// <param name="parentControl"></param>
            public static void ClearWhereString(Control parentControl)
            {
                if (parentControl == null)
                    throw new ArgumentNullException("装载查询控件的容器不存在!");

                foreach (Control control in parentControl.Controls)
                {
                    if (control is IExtControl)
                    {
                        if (control.Enabled)
                        {
                            IExtControl extControl = (IExtControl)control;
                            extControl.ClearDataValue();
                        }
                    }
                }
            }
            /// <summary>
            /// 读取生成查询条件
            /// </summary>
            /// <param name="parentControl"></param>
            /// <returns></returns>
            public static string GetWhereString(Control parentControl)
            {
                return GetWhereString(parentControl, "And");

            }

            /// <summary>
            /// 读取查询条件(web直接处理传回的XML，或者JSON字符串)
            /// </summary>
            /// <param name="parentControl"></param>
            /// <param name="logic"></param>
            /// <returns></returns>
            public static string GetWhereString(Control parentControl, string logic)
            {
                if (parentControl == null)
                    throw new ArgumentNullException("装载查询控件的容器不存在!");

                string whereStr = "";
                foreach (Control control in parentControl.Controls)
                {
                    if (control is IExtControl)
                    {
                        IExtControl extControl = (IExtControl)control;
                        if (extControl.ExtDataValue.Trim() != "")
                            whereStr += " (" + GetMatch(extControl.ExtDataField, extControl.ExtQueryExpression.ToString(), extControl.ExtDataValue) + ") " + logic;
                    }

                }
                if (whereStr == "")
                    return "";
                else
                    return " where " + whereStr.Substring(0, whereStr.Length - logic.Length);

            }


            /// <summary>
            /// 根据匹配域，匹配方法，匹配域值取得匹配条件串
            /// </summary>
            /// <param name="fieldName">匹配域名称(同数据库一致)</param>
            /// <param name="match">匹配方法(null=EQ)</param>
            /// <param name="fieldValue">匹配域的值</param>
            /// <returns>域的条件串</returns>
            public static string GetMatch(string fieldName, string match, string fieldValue)
            {
                string mop = "=";
                fieldValue = fieldValue.Replace("'", "''");
                if (AttrIsNull(match))
                    match = "EQ";
                switch (match.ToUpper())
                {
                    case "EQ":
                        mop = fieldName + "='" + fieldValue + "'";
                        break;
                    case "UE":
                        mop = fieldName + "<>'" + fieldValue + "'";
                        break;
                    case "GT":
                        mop = fieldName + ">'" + fieldValue + "'";
                        break;
                    case "LT":
                        mop = fieldName + "<'" + fieldValue + "'";
                        break;
                    case "IN":
                        string[] flds = fieldValue.Split(',');
                        string fldstr = "";
                        for (int i = 0; i < flds.Length; i++)
                            fldstr += "'" + flds[i] + "',";
                        fldstr = fldstr.Substring(0, fldstr.Length - 1);
                        mop = fieldName + " IN (" + fldstr + ")";
                        break;
                    case "FR":
                        mop = fieldName + ">='" + fieldValue + "'";
                        break;
                    case "TO":
                        mop = fieldName + "<='" + fieldValue + "'";
                        break;
                    case "LK":
                        mop = fieldName + " LIKE '%" + fieldValue + "%'";
                        break;
                    case "SK":
                        string[] flds0 = fieldValue.Split(' ');
                        string fldstr0 = "%";
                        for (int i = 0; i < flds0.Length; i++)
                            if (flds0[i].Trim() != "")
                                fldstr0 += flds0[i] + "%";
                        mop = fieldName + " LIKE '" + fldstr0 + "'";
                        break;
                    case "NOTNULL":
                        mop = fieldName + " IS NOT NULL and " + fieldName + " <>'' ";
                        break;
                    case "ISNULL":
                        mop = fieldName + " IS NULL or " + fieldName + " ='' ";
                        break;
                    case "OR":
                        string[] flds1 = fieldValue.Split(' ');
                        string fldstr1 = "";
                        for (int i = 0; i < flds1.Length; i++)
                            if (flds1[i].Trim() != "")
                                fldstr1 += fieldName + " Like '%" + flds1[i] + "%' or ";
                        fldstr1 = fldstr1.Substring(0, fldstr1.Length - 3);
                        mop = " (" + fldstr1 + ") ";
                        break;
                    case "YEARANDMONTH":
                        mop = string.Format("YEAR({0})=YEAR('{1}') and MONTH({0})=MONTH('{1}')", fieldName, fieldValue);
                        break;
                }
                return mop;
            }

            public static bool AttrIsNull(string attr)
            {
                if (attr == null || attr == "")
                    return true;
                else
                    return false;
            }

        }
   
}
